package com.mcx.modules.proto;

import com.mcx.modules.common.IProto;
import com.mcx.modules.common.MMO_DataStream;

import java.io.*;
import java.util.*;

/**
 * 服务器返回角色学会的技能
 */
public class RoleData_SkillReturnProto implements IProto
{
    private static MMO_DataStream dataStream = new MMO_DataStream();

    @Override
    public short getProtoCode() { return 11001; }

    public Integer RoleId; //角色Id
    public byte SkillCount; //学会的技能数量
    public ArrayList<SkillData> CurrSkillDataList; //当前学会的技能

    /**
     * 当前学会的技能
     */
    public static class SkillData
    {
        public Integer SkillId; //技能编号
        public Integer SkillLevel; //技能等级
        public byte SlotsNo; //技能槽编号
    }

    public byte[] ToArray() {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try (DataOutputStream outputStream = new DataOutputStream(out)) {
            dataStream.outputStream = outputStream;
            dataStream.WriteShort(getProtoCode());
            dataStream.WriteInt(RoleId);
            dataStream.WriteByte(SkillCount);
            for (int i = 0; i < SkillCount; i++) {
                dataStream.WriteInt(CurrSkillDataList.get(i).SkillId);
                dataStream.WriteInt(CurrSkillDataList.get(i).SkillLevel);
                dataStream.WriteByte(CurrSkillDataList.get(i).SlotsNo);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return out.toByteArray();    }
    public static RoleData_SkillReturnProto GetProto(byte[] buffer) {
        RoleData_SkillReturnProto proto = new RoleData_SkillReturnProto();
        try (DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(buffer))) {
            dataStream.inputStream = inputStream;
            proto.RoleId = dataStream.ReadInt();
            proto.SkillCount = (byte)dataStream.ReadByte();
            proto.CurrSkillDataList = new ArrayList<SkillData>();
            for (int i = 0; i < proto.SkillCount; i++) {
                SkillData _CurrSkillData = new SkillData();
                _CurrSkillData.SkillId = dataStream.ReadInt();
                _CurrSkillData.SkillLevel = dataStream.ReadInt();
                _CurrSkillData.SlotsNo = (byte)dataStream.ReadByte();
                proto.CurrSkillDataList.add(_CurrSkillData);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return proto;
    }
}